export function debounce(fn, delay = 1000){
	var timer
	return function(){
		let args = arguments
		let ctx = this
		clearTimeout(timer)
		timer = setTimeout(()=>{
			fn.apply(ctx, args)
		}, delay)
	}
}
export function getDateString(date){
	if(!(date instanceof Date)){
		return ''
	}
	let year = date.getFullYear()
	let month = date.getMonth() + 1
	let day = date.getDate()
	month = month > 9 ? month : '0' + month
	day = day >  9 ? day : '0' + day
	return `${year}-${month}-${day}`
}
export function getQueryObject(url) {
  url = url == null ? window.location.href : url
  const search = url.substring(url.lastIndexOf('?') + 1)
  const obj = {}
  const reg = /([^?&=]+)=([^?&=]*)/g
  search.replace(reg, (rs, $1, $2) => {
    const name = decodeURI($1)
    let val = decodeURI($2)
    val = String(val)
    obj[name] = val
    return rs
  })
  return obj
}


// 洗牌算法
export function shuffle(arr) {
  for (let i = arr.length - 1; i >= 0; i--) {
	const randomIndex = Math.floor(Math.random() * (i + 1))
	const itemAtIndex = arr[randomIndex]
	arr[randomIndex] = arr[i]
	arr[i] = itemAtIndex
  }
  return arr
}
//洗牌算法2
export const shuffle2 = (arr) => {
	var result = [],
		random;
	while (arr.length > 0) {
		random = Math.floor(Math.random() * arr.length);
		result.push(arr[random])
		arr.splice(random, 1)
	}
	return result;
}